home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 563 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.3 KB

  1. Path: cs.uwa.edu.au!jasonb
  2. From: jasonb@cs.uwa.edu.au (Jason S Birch)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: PPC compilers
  5. Date: 9 Jan 96 07:51:19 GMT
  6. Organization: The University of Western Australia
  7. Message-ID: <jasonb.821173879@cs.uwa.edu.au>
  8. References: <john.hendrikx.40ka@grafix.xs4all.nl> <jasonb.820051107@cs.uwa.edu.au> <4c9i2l$h3i@sunsystem5.informatik.tu-muenchen.de> <4ck47h$g07@maureen.teleport.com> <19960106.4EE928.CF59@sisyphus.demon.co.uk> <4cokkg$415@maureen.teleport.com> <19960107.533250.14585@sisyphus.demon.co.uk> <4cqrti$f6u@maureen.teleport.com>
  9. NNTP-Posting-Host: decadence.cs.uwa.oz.au
  10. X-Newsreader: NN version 6.5.0 #3 (NOV)
  11.  
  12. sschaem@teleport.com (Stephan Schaem) writes:
  13.  
  14. > polymorphism?
  15.  
  16. > int a,b,c;
  17.  
  18. > ...
  19. > a = b /c;
  20.         ^
  21. > a *= result;
  22.     ^
  23. The "/" and "*" are polymorphic - you can use them in any mathematical
  24. expression, without caring whether the left and right sides are chars,
  25. shorts, longs, floats, doubles, or any equivalent type. If they were
  26. not polymorphic, you might have, for example:
  27. a int= b int/ c;
  28. a int*= result;
  29.  
  30. This is essentially what you have in assembler. Note that with C++,
  31. you can overload "+", "-", "*", "/", "=", etc, to work with any custom
  32. classes as well, so you could have, for example:
  33.  
  34. vector a,b,c;
  35. real r;
  36. ...
  37. a = b + c;
  38. r = a * b;
  39. cout << "Values are: " << a << ", " << r << ".\n";
  40.  
  41. and have, say:
  42.  
  43. Values are: [3,7,3], 67.
  44.  
  45. as the output.
  46.  
  47. > struct.float = a;
  48.  
  49. This is polymorphism of "=", but it's also taking advantage of the
  50. fact that C can convert an integer into a float.
  51.  
  52. > I dunno , I just find it puzzling to declar variable without giving
  53. > a care of what its usage will be. You programing practice is VERY unwise...
  54.  
  55. Your misunderstanding is that you think he means he has no idea of
  56. what types each variable is, whereas what he's saying is he need have
  57. no idea of how each type is actually implemented on a particular
  58. machine/OS - all he has to do is know how those types are defined to
  59. behave under ANSI C. In fact, if you want to write portable code, you
  60. have to restrict your knowledge of behaviour of the types to that
  61. which is guaranteed under ANSI C. Taking advantage of any other
  62. information you might have (eg. endian-ness, number of bits) limits 
  63. your portability.
  64.  
  65. >: I know better than to declare a value as unsigned long when I'm
  66. >: going to assign a clock_t to it.  And if I did, I'd lose the
  67. >: fractional part of a floating-point clock_t, not the upper 32 bits.
  68.  
  69. > You said yourself after peeking at the .h that clock_t was a ulong.
  70. > But you know better then not using a ulong... ? Should I assume
  71. > you alway use the largest data type available,double ? (What a waist)
  72.  
  73. No, you should use clock_t! If you want a variable to use in functions
  74. that expect a clock_t, you declare it thus:
  75.  
  76. clock_t Stephens_var;
  77.  
  78. You do *not* declare it as:
  79.  
  80. ulong Stephens_var;
  81.  
  82. even if you *know* that on your particular implementation, that's what
  83. it really is, because that restricts your portability.
  84.  
  85. > And you mean you would loose the fractional part of clock_t if you
  86. > used ulong , not the upper 32bits? This make absolutly no sense since
  87. > we are talking about interger types.(BTW copy 0x00000001 into a 2byte int,
  88. > you will loose the upper 16bit... )
  89.  
  90. You lose nothing if you declare it as clock_t.
  91.  
  92. > If the header file was programmed correctly it would not use ANSI data
  93. > type directly.... 
  94.  
  95. Why not? You should just know better than to look at it and take
  96. advantage of that information.
  97.  
  98. Incidentally, when I create a custom data type I want to keep private,
  99. I provide a public header file similar to:
  100.  
  101. typedef void *MyDataType;
  102.  
  103. MyDataType *MDT_Create(...);
  104. void MDT_Dispose(MyDataType *);
  105.  
  106. and inside the private header file, I actually define what MyDataType
  107. is. This way, no-one can "accidentally" use the internal definition,
  108. and I am free to change it at will, without having to worry about
  109. those who *don't* know better than to look at it and take advantage of
  110. the information.
  111.  
  112. > The way you get optimal speed is optimizing the optimal algorithim.
  113. > If you stop at the algorithm you loose... Optimizing at this stage
  114. > will give an exponential result on the work you done on the algorithm.
  115. > And usually this stage is done in assembler.
  116.  
  117. The problem is, do you want portable code? If yes, then you should be
  118. aware that what might be optimal for one particular CPU using one
  119. particular compiler is not necessarily the same for another. For
  120. example, scanning through an array and adding the contents using array
  121. dereferencing and a moving pointer: On an 040 Amiga, the latter was
  122. faster; using gcc on a Sun, both produced the same assembler code; and
  123. using Metrowerks on a PowerMac, the former was actually quicker
  124. because the pointer method used post-incrementing (ie. sum += *ptr++)
  125. and the compiler wasn't smart enough to realize it didn't need to
  126. store the pre-incremented value and used an extra register to store
  127. it. The moral? Don't make assumptions about what is an optimization,
  128. and what isn't, unless you test it on every combination you plan to
  129. run it on.
  130.  
  131. > Stephan
  132.  
  133. -- 
  134. Jason S Birch                        ,-_|\ email: jasonb@cs.uwa.edu.au
  135. Department of Computer Science      /     \ Tel (work): +61 9 380 1840
  136. The University of Western Australia *_.-._/ Fax (work): +61 9 380 1089
  137. Nedlands  W. Australia  6907             v  Tel (home): +61 9 386 8630
  138.